// pages/address/index.js
import http from '../../../../service/http.js'

import tips from '../../../../class/Utils/Tips'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    adds_status: 1,
    jlist: [],
    index: 0,
    cart_price: 0,
    liuyan: '',
    tishi_youhuijuan: "暂无可用",
    sy_youhuijuan: 0,
    jid: 0,
    pickerdisabled: true,
    juan_val: 0,
    totalprice: 0,
    minus: 0
  },

  onLoad: function (get) {
    this.setData({
      lj: get.lj ? get.lj : 0
    })


  },
  /* 生命周期函数--监听页面加载 */
  onShow: function (options) {

    //获取高度宽度等
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var pageWidth = res.windowWidth - 20;
        var pjvieww = pageWidth * 0.25
        that.setData({
          scrollHeight: res.windowHeight,
          pjuserpicliH: pjvieww
        });
      }
    });
    if (that.data.lj == '1') {
      var cart = wx.getStorageSync('lj_cart');
      that.setData({
        carts: cart,
      });
      var cart_price = wx.getStorageSync('ljcart_price')

      if (cart[0].pro[0].is_yhq == 1) {
        var yhq_price = cart_price

      } else {
        var yhq_price = 0.00
      }
      that.setData({
        cart_price: cart_price ? cart_price : 0.00,
        totalprice: cart_price
      })
      var cart_count = wx.getStorageSync('ljcart_count')
      that.setData({
        cart_count: cart_count ? cart_count : 0
      });
    } else {
      var cart = wx.getStorageSync('cart');
      var yao = [{ "pro": [] }]
      if (cart) {
        for (var i = 0; i < cart[0].pro.length; i++) {
          if (cart[0].pro[i].select == 'success') {
            yao[0].pro.push(cart[0].pro[i])
          }
        }

        that.setData({
          carts: yao
        });

      } else {
        that.setData({
          carts: [],
        });
      }
      var cart_price = wx.getStorageSync('cart_price')
      var yhq_price = wx.getStorageSync('yhq_price')
      that.setData({
        cart_price: cart_price ? cart_price : 0.00,
        totalprice: cart_price
      })
      var cart_count = wx.getStorageSync('cart_count')
      that.setData({
        cart_count: cart_count ? cart_count : 0
      });
    }

    if (that.data.jid == 0 && 1==0) {  //优惠券暂时不做
      http.req("/Userjifen/tjorder_youhuijuan", { uid: wx.getStorageSync('user').userid, price: yhq_price }, function (res) {
        console.log(res)
        var thiscode = res.code
        var juanlength = res.count
        that.setData({
          youhuijuan: res.code,
          jlist: res.listlist
        })
        if (thiscode == 1) {
          that.setData({
            tishi_youhuijuan: juanlength + '张可用',
            pickerdisabled: false,
          })
        } else {
          that.setData({
            tishi_youhuijuan: '暂无可用',
            pickerdisabled: true,
          })
        }
      })
    }

    /*
          查找默认地址
          */
    var user = wx.getStorageSync('user')
    http.req('/Wxuser/mr_adds', { userid: user.userid }, function (res) {
      console.log('默认地址', res)
      if (res.status == '1') {
        that.setData({
          adds: res.data,
          adds_status: 1
        })
        wx.setStorageSync('adds', res.data);
      } else {
        that.setData({
          adds_status: 2
        })
      }
    })



  },
  // 选择优惠券  暂时不做
  xz_youhuijuan1: function (e) {
    var that = this
    var index = e.detail.value;
    var list = that.data.jlist
    var cart_price = that.data.cart_price


    var cp = (cart_price - list[index].hou).toFixed(2)

    var minus = (cart_price - cp).toFixed(2)
    that.setData({
      tishi_youhuijuan: list[index].jname,
      sy_youhuijuan: 1,
      juan_val: list[index].hou,
      totalprice: cp,
      jid: list[index].id,
      minus: minus
    })

  },
  //提交表单
  tijiaopay: function () {
    var that = this
    if (that.data.lj == '1') {
      var cart = wx.getStorageSync('lj_cart');
    } else {
      var cart = wx.getStorageSync('cart');
    }

    var adds = wx.getStorageSync("adds");


    if (!adds) {
      wx.showModal({
        title: '提示',
        content: '请选择地址',
        showCancel: false,
        confirmText: '确认',
        success: function (res) {
          if (res.confirm) {

            wx.navigateTo({
              url: '/pages/tjaddress/index'
            })

          }
        }
      })
      return false;
    }

    var user = wx.getStorageSync('user');
    var userinfo = wx.getStorageSync('userinfo');

    var totalPrice = that.data.totalprice;
    if (totalPrice == 0.00) {
      wx.showModal({
        title: '温馨提示',
        showCancel: false,
        content: '购物车中未选择商品'
      })
      return false;

    }
    var liuyan = that.data.liuyan;




    var data = {
      cart: JSON.stringify(cart),
      adds: JSON.stringify(adds),
      userid: user.userid,
      nickname: userinfo.nickName,
      totalPrice: totalPrice,
      openid: user.openid,
      pay_status: 1,
      liuyan: liuyan,
      sy_youhuijuan: that.data.sy_youhuijuan,
      jid: that.data.jid,
      juan_val: that.data.juan_val
    }


    http.req('/Wxorder/add_order', data, function (oidres) {
      console.log("订单返回详情", oidres)

      if (oidres.pay.state == '1') {
        wx.requestPayment({
          'timeStamp': oidres.pay.timeStamp,
          'nonceStr': oidres.pay.nonceStr,
          'package': oidres.pay.package,
          'signType': 'MD5',
          'paySign': oidres.pay.paySign,
          'success': function (res) {
            console.log("111");
            wx.showToast({
              title: "支付成功",
              icon: 'success',
              duration: 2000
            })
            //付款结束，清空购物车
            wx.removeStorageSync('cart')
            wx.removeStorageSync('cart_price')
            wx.removeStorageSync('yhq_price')
            wx.removeStorageSync('cart_count')
            http.req('/Wxorder/pay_ok', {
              orderid: oidres.orderid,
              userid: user.userid
            }, function (okres) {
              console.log(okres)
              if (okres.status == '1') {
                wx.redirectTo({
                  url: '/pages/order/index/index'
                })
              }
            })


          },
          'fail': function (res) {
            console.log("222");
            wx.showToast({
              title: "取消支付",
              icon: 'fail',
              duration: 2000
            })
            http.req('/Wxorder/pay_cancel', { orderid: oidres.orderid }, function (cancleres) {
              if (cancleres.status == '1') {

              }

            })
          }
        })

      } else {
        wx.showToast({
          title: oidres.pay.Msg,
          icon: 'fail',
          duration: 1000
        })
      }

    })






  },
  tijiaopaytest: function () {
    var that = this
    var cart = wx.getStorageSync('cart');
    var adds = wx.getStorageSync("adds");
    var user = wx.getStorageSync('user');
    var userinfo = wx.getStorageSync('userinfo');
    var totalPrice = wx.getStorageSync('cart_price');
    var totalPrice = that.data.cart_price;
    var data = {
      cart: JSON.stringify(cart),
      adds: JSON.stringify(adds),
      userid: user.userid,
      nickname: userinfo.nickName,
      totalPrice: totalPrice,
      openid: user.openid

    }

    http.req('/Wxorder/add_order', data, function (res) {
      console.log(res)
      // return;
      var timeStamp = (Date.parse(new Date()) / 1000).toString();
      wx.requestPayment({
        'timeStamp': timeStamp,
        'nonceStr': res.pay.nonceStr,
        'package': res.pay.package,
        'signType': 'MD5',
        'paySign': res.pay.paySign,
        'success': function (res) {
          console.log("zhifu");
        },
        'fail': function (res) {
          console.log("zhifu");
        }
      })

    })

  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = e.detail.value;
    this.setData({
      index: index
    })
    console.log(index);
    var cart_price = wx.getStorageSync('cart_price');

    this.setData({
      cart_price: cart_price,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})